<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Session;


class Access extends Admin
{

    // 查询数据
    public function index(Request $req,$page=null)
    {
            $res = $req->get();

            if ($page == null) {

                $num = 0;

                $like = $res['title'];

                $sql = DB::table('access')->where('title','like','%'.$like.'%')->limit($num,5)->select();

                $data = DB::table('access')->where('title','like','%'.$like.'%')->select();

                $count = count($data);

                $all = ceil($count/5);

                return view('admin@access/accessList',[
                    'data' => $sql,
                    'all' => $all
                ]);

            }else{
                $num = ($page-1)*5;

                $sql = DB::table('access')->limit($num,5)->select();

                $data = DB::table('access')->select();

                $count = count($data);

                $all = ceil($count/5);

                return view('admin@access/accessList',[
                    'data' => $sql,
                    'all' => $all
                ]);
            }

    }
    // public function doindex()
    // {
    //     $sql = DB::table('access')->limit('0,4')
    //             ->select();
    //     $data = DB::table('access')->select();
    //     $count = count($data);
    //     $all = ceil($count/4);
    //     // var_dump($c);die;

    //     return view('admin@access/accessList',[
    //         'data' => $sql,
    //         'all' => $all
    //     ]);
    // }

    // 跳转添加数据页面
    public function add()
    {
        return view('admin@access/addAccess');
    }

    // 保存数据
    public function save(Request $req)
    {

        // 接收数据
        $data = $req->get();


        // 执行sql
        $result = Db::table('access')->data($data)->insert();

        // 返回结果
        if ($result > 0) {

             return $this->success('添加成功..', url('admin/Access/index'));

        } else {

             return $this->error('添加失败,请重试...');

        }


    }


    // 查询语句(修改查询)
    public function find($id)
    {

        // 查询
        $data = Db::table('access')->find($id);

        // 跳转
        return view('admin@access/edit', ['data' => $data]);

    }


    // 修改管理员资料
    public function updata(Request $request)
    {

        // 接收数据
        $data = $request->get();


        // 获取id
        $id = $request->get('id');

        // 执行sql语句


        $result = Db::table('access')->where('id', $id)->update($data);

        // 返回
       if ($result > 0) {

           return $this->success('保存成功..', url('admin/Access/index'));

       } else {

           return $this->error('保存失败,请重试...');

       }
    }



    // 权限删除
    public function del_access()
    {
        $id = input('id');

        // 删除角色权限表的指定数据
        $result = Db::table('role_access')->where('access_id', $id)->delete();

        // 删除权限
        $res = Db::table('access')->where('id ='.$id)->delete();

        // 返回结果
        if ($res > 0) {

             return $this->success($res, url('admin/User/access'));

        } else {

             return $this->error($res);

        }

    }

}


